package youxiao.bytezon.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import youxiao.bytezon.models.Teacher;
import youxiao.bytezon.repositories.TeacherRepository;

public class BaseController {
	
	@Autowired
	TeacherRepository teacherRepository;
	
	protected Teacher getCurrentTeacher() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String currentPrincipalName = authentication.getName();
		return teacherRepository.findByPhone(currentPrincipalName);
	}
	
	protected void canCurrentTeacherAccess(Integer teacherId) throws Exception {
		Teacher teacher = getCurrentTeacher();
		if ( teacher.getId() == teacherId ) {
			throw new Exception("Current teacher can not access this resource");
		}
	}
	
}
